package com.eastcom.groovy.provider

import com.eastcom.groovy.config.TaskConfig
import com.eastcom.groovy.config.TaskConfigList
import com.eastcom.groovy.task.Task
import com.eastcom.groovy.tools.file.JaxbUtil;

import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import com.eastcom.groovy.context.*

import javax.xml.bind.JAXBException
import javax.xml.datatype.XMLGregorianCalendar

/**
 * Created by yuanyue on 2014-08-02.
 */
class TaskProvider{
	private static Log logger = LogFactory.getLog(TaskProvider.class);

	public TaskProvider() {
		super();
	}

	def loadTaskClosure={ path->
		path.each {
			def root = JaxbUtil.converyFileToJavaBean(it,"UTF-8", TaskConfigList.class);
			if(root == null){
				return false;
			}
			TaskConfigList taskConfigList = root
			String temp = it
			String rootPath = new File(temp).getParent()
			println rootPath+"/"+taskConfigList.beanConfigFile
			if(taskConfigList.beanConfigFile != null && ! taskConfigList.beanConfigFile.equals("")){
				DynamicBeanDomianProvider.loadXmlConfigFile("/"+rootPath+"/"+taskConfigList.beanConfigFile)
			}
			List<TaskConfig> parseTasks = taskConfigList.taskConfigList
			for(int i = 0; i < parseTasks.size(); i++){
				Task task = new Task()
				TaskConfig taskConfig = parseTasks.get(i)
				task.jobId = taskConfig.type+"_"+taskConfig.name
				task.taskConfig = taskConfig
				task.cronExpression = taskConfig.cron
				task.jobGroup = taskConfig.type
				task.jobName = taskConfig.name
				task.jobStatus = "1"
				task.rootPath = rootPath
				TaskContext.addTask(task)
				taskConfig.initConfig(task.jobId,rootPath)
			}
		}
	}

	def getLoadTaskClosure() {
		return loadTaskClosure
	}

	void setLoadTaskClosure(loadTaskClosure) {
		this.loadTaskClosure = loadTaskClosure
	}
}
